package com.meixiang.adapter.shopping;

import android.content.Context;
import android.content.Intent;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.ImageView;
import android.widget.TextView;

import com.meixiang.R;
import com.meixiang.activity.homes.service.ShowPictureActivity;
import com.meixiang.entity.services.Evaluate;
import com.meixiang.entity.shopping.GoodsEvaluateEntity;
import com.meixiang.entity.shopping.result.GoodsCommentResult;
import com.meixiang.tool.Tool;
import com.meixiang.util.GlideHelper;
import com.meixiang.view.EvaluateView;
import com.meixiang.view.LineView;
import com.meixiang.view.StarLayout;
import com.meixiang.view.flowlayout.CustomFlowAdapter;
import com.meixiang.view.flowlayout.CustomFlowLayout;

import java.util.ArrayList;
import java.util.List;

import butterknife.Bind;
import butterknife.ButterKnife;

/**
 * 商品评价列表适配器
 *
 * @author LZRUI
 */
public class GoodsEvaluateAdapter extends BaseAdapter {

    enum ITEM {
        HEADER,
        NORMAL
    }

    private List<GoodsEvaluateEntity> evaluateList;
    private List<Evaluate> tagList;
    private String scoresText;
    private String scores;
    private Context mContext;

    public GoodsEvaluateAdapter(Context context) {
        this.mContext = context;
    }

    public void refreshData(GoodsCommentResult result) {
        this.evaluateList = result.getEvaluateList();
        this.tagList = result.getGoodsTagList();
        this.scores = result.getScores();
        this.scoresText = result.getScoresText();
        notifyDataSetChanged();
    }

    @Override
    public int getCount() {
        return evaluateList == null ? 0 : evaluateList.size() + 1;
    }

    @Override
    public Object getItem(int position) {
        return position == 0 ? tagList : evaluateList.get(position);
    }

    @Override
    public long getItemId(int position) {
        return position;
    }

    @Override
    public int getViewTypeCount() {
        return 2;
    }

    @Override
    public int getItemViewType(int position) {
        return position == 0 ? ITEM.HEADER.ordinal() : ITEM.NORMAL.ordinal();
    }

    @SuppressWarnings("unchecked")
    @Override
    public View getView(int position, View convertView, ViewGroup parent) {
        if (position == 0) {
            convertView = LayoutInflater.from(parent.getContext()).inflate(R.layout.layout_goods_evaluate_header, parent, false);
            TagViewHolder tagViewHolder = new TagViewHolder(convertView);
            tagViewHolder.starLayout.setStarNumber(Integer.valueOf(scores)+"");
            tagViewHolder.starLayout.setStarModel(StarLayout.MODEL_SHOW_All);
            tagViewHolder.tvEvaluate.setText(scoresText);
            tagViewHolder.evaluateView.setData(tagList);
        } else {
            ViewHolder viewHolder;
            if (convertView == null) {
                convertView = LayoutInflater.from(parent.getContext()).inflate(R.layout.list_item_goods_evaluate, parent, false);
                viewHolder = new ViewHolder(convertView);
                convertView.setTag(viewHolder);
            } else {
                viewHolder = (ViewHolder) convertView.getTag();
            }
            if (evaluateList.size() != position) {
                viewHolder.line.setMarginLeft(15);
            }
            GoodsEvaluateEntity entity = evaluateList.get(position - 1);
            if (entity != null) {
                GlideHelper.showCircleImage(parent.getContext(), entity.getMemberAvatar(), R.mipmap.default_head_portrait, viewHolder.ivHeadPortrait);
                viewHolder.tvName.setText(entity.getGevalFrommembername());
                viewHolder.tvEvaluate.setText(entity.getGevalContent());
                viewHolder.tvTime.setText(entity.getCreateTime());
                viewHolder.flowImage.setMargin(0, 5, 5, 0);
                final ArrayList<String> urls = entity.getCommentImages();
                if (urls == null || urls.size() == 0) {
                    viewHolder.flowImage.setVisibility(View.GONE);
                } else {
                    viewHolder.flowImage.setVisibility(View.VISIBLE);
                    viewHolder.flowImage.setAdapter(new CustomFlowAdapter(urls) {
                        @Override
                        public View getView(CustomFlowLayout parent, int position, Object o) {
                            View layout = LayoutInflater.from(parent.getContext()).inflate(R.layout.list_item_goods_evaluate_image, parent, false);
                            ImageView imageView = (ImageView) layout.findViewById(R.id.iv_image);
                            GlideHelper.showImage(mContext, (String) o, R.drawable.image_default_white_bg, imageView);
                            return layout;
                        }
                    });
                    viewHolder.flowImage.setOnFlowItemClickListener(new CustomFlowLayout.OnFlowItemClickListener() {
                        @Override
                        public void onItemClick(int position, View view) {
                            if (Tool.isFastDoubleClick()) return;
                            Intent intent = new Intent(mContext, ShowPictureActivity.class);
                            intent.putStringArrayListExtra("urls", urls);
                            intent.putExtra("position", position);
                            mContext.startActivity(intent);
                        }
                    });
                }
            }
        }
        return convertView;
    }

    static class ViewHolder {
        @Bind(R.id.iv_head_portrait)
        ImageView ivHeadPortrait;
        @Bind(R.id.tv_name)
        TextView tvName;
        @Bind(R.id.tv_time)
        TextView tvTime;
        @Bind(R.id.tv_evaluate)
        TextView tvEvaluate;
        @Bind(R.id.flow_image)
        CustomFlowLayout flowImage;
        @Bind(R.id.line)
        LineView line;

        ViewHolder(View view) {
            ButterKnife.bind(this, view);
        }
    }

    static class TagViewHolder {
        @Bind(R.id.star_layout)
        StarLayout starLayout;
        @Bind(R.id.tv_evaluate)
        TextView tvEvaluate;
        @Bind(R.id.evaluate_view)
        EvaluateView evaluateView;

        TagViewHolder(View view) {
            ButterKnife.bind(this, view);
        }
    }
}
